package com.lanou3g.demo.dao;

import com.lanou3g.demo.db.DBConnector;
import com.lanou3g.demo.db.EntityHandler;
import com.lanou3g.demo.entity.UserEntity;
import org.apache.commons.dbutils.QueryRunner;

import java.sql.Connection;
import java.sql.SQLException;

public class UserDao {

    // 向数据库中插入用户信息
    public static void addUser(String uid,String username,String password,String email,String code) throws SQLException {
        Connection conn = DBConnector.getInstance().getConnection();
        QueryRunner runner = new QueryRunner();
        String sql = "insert into tb_user value (?,?,?,?,?,null)";
        runner.update(conn,sql,uid,username,password,email,code);
    }

    // 根据用户名，看是否存在
    public static UserEntity selectUser(String username) throws SQLException {
        Connection conn = DBConnector.getInstance().getConnection();
        QueryRunner runner = new QueryRunner();
        String sql="select * from tb_user where username=?";
        UserEntity userEntity = runner.query(conn, sql, new EntityHandler<>(UserEntity.class), username);
        return userEntity;
    }

}
